package com.fz.controller;

import com.sun.org.apache.regexp.internal.RE;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by "鐤媯鐨勭爜宸�" on 2017-09-05.
 */
@Controller
@RequestMapping("/upload.do")
//单文件上传
public class UpLoud {
    @RequestMapping("/to") @ResponseBody
    public void up(@RequestParam("fe") CommonsMultipartFile file, String name, HttpServletRequest req) throws IOException {
    //设置时间
    String s=new SimpleDateFormat("yyMM").format(new Date());
    //选择路径
    String path="/upload/"+s;
    //获取真实路径及时传惨
    String p=req.getServletContext().getRealPath(path);
    //创建文档
        File ff=new File(p);
        if(!ff.exists()){
            ff.mkdirs();
        }
       String fliename=file.getOriginalFilename();
        file.transferTo(new File(p,fliename));
    }


        @RequestMapping("/too") @ResponseBody
        public ModelAndView load(@RequestParam("fe") CommonsMultipartFile file, String name, HttpServletRequest req) throws IOException {
        ModelAndView mv= new ModelAndView("mv");
        mv.addObject("name",file.getOriginalFilename());
        mv.addObject("size",file.getSize());
        mv.addObject("fromname",file.getName());
        mv.addObject("con",file.getContentType());

            return mv;
        }
        //多文件上传
        @RequestMapping("/multi.do") @ResponseBody
        //commonsMultiartFile是对接收到的至进行类型声明
        public void mu(@RequestParam("fs") CommonsMultipartFile[] fi ) throws IOException {
        for (CommonsMultipartFile f:fi){
            f.transferTo(new File("c:/",f.getOriginalFilename()));
        }

        }

        }
